package cn.sciento.starter.lock.factory;

import java.util.EnumMap;

import cn.sciento.core.convertor.ApplicationContextHelper;
import cn.sciento.starter.lock.enums.LockType;
import cn.sciento.starter.lock.service.LockService;
import cn.sciento.starter.lock.service.impl.FairLockServiceImpl;
import cn.sciento.starter.lock.service.impl.ReadLockServiceImpl;
import cn.sciento.starter.lock.service.impl.RedLockServiceImpl;
import cn.sciento.starter.lock.service.impl.ReentrantLockServiceImpl;
import cn.sciento.starter.lock.service.impl.WriteLockServiceImpl;

public class LockServiceFactory {
  private static final EnumMap<LockType, Class<?>> serviceMap = new EnumMap<>(LockType.class);
  
  static {
    serviceMap.put(LockType.REENTRANT, ReentrantLockServiceImpl.class);
    serviceMap.put(LockType.FAIR, FairLockServiceImpl.class);
    serviceMap.put(LockType.READ, ReadLockServiceImpl.class);
    serviceMap.put(LockType.WRITE, WriteLockServiceImpl.class);
    serviceMap.put(LockType.RED, RedLockServiceImpl.class);
  }
  
  public LockService getLock(LockType lockType) {
    return (LockService) ApplicationContextHelper.getContext().getBean(serviceMap.get(lockType));
  }
}
